<%@ page import="edu.uiuc.ncsa.GetFileResult" %>
<%@ page import="edu.uiuc.ncsa.ResponseHandler" %>
<%@ page import="edu.uiuc.ncsa.WebUtils" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="java.io.File" %>
<%--
 @author : Sachith Dhanushka (sachith.dhanushka@gmail.com)
 @date : Jun 10, 2008 - 10:14:35 PM
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%
    String action = request.getParameter("action");
    String output = request.getParameter("output");

    Context env = (Context) new InitialContext().lookup("java:comp/env");
    String tempFolderLoc = (String) env.lookup("TempFolder");

    if ("ShpFile2kml".equals(action) && ServletFileUpload.isMultipartContent(request)) {

        WebUtils.convertShapeFile(request, response, output, tempFolderLoc);

    } else if ("WOF2KML".equals(action)) {
        String siteId = request.getParameter("siteID");
        response.sendRedirect("http://wron.net.au/DemosII/DataStreamers/ODMtoKML.aspx?Mode=1&SiteCode=" + siteId + "&URL=http://river.sdsc.edu/wateroneflow/NWIS/UnitValues.asmx?WSDL");
    } else if ("KML2RDF".equals(action)) {

        try {
            GetFileResult result = WebUtils.getSourceFile(request, tempFolderLoc);
            File inputKmlFile = result.getInputFile();
            String fileURL = result.getUrl();

            System.out.println("inputKmlFile = " + inputKmlFile.getAbsolutePath());
            // send the rdf file
            File rdfFile = WebUtils.convertKMLToRDF(inputKmlFile, fileURL);
            ResponseHandler.sendTextFileToBrowser(rdfFile, response);
            if (rdfFile.isFile()) {
                rdfFile.delete();
            }
            inputKmlFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseHandler.sendError(response, "Error in processing the request. " + e.getMessage());
        }
    } else if ("RDF2KML".equals(action)) {

        try {
            GetFileResult result = WebUtils.getSourceFile(request, tempFolderLoc);
            File inputRDFFile = result.getInputFile();
            String fileURL = result.getUrl();

            System.out.println("inputRDFFile = " + inputRDFFile.getAbsolutePath());
            // send the rdf file
            File rdfFile = WebUtils.convertRDFToKML(inputRDFFile);
            ResponseHandler.sendTextFileToBrowser(rdfFile, response);
            if (rdfFile.isFile()) {
                rdfFile.delete();
            }
            inputRDFFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseHandler.sendError(response, "Error in processing the request. " + e.getMessage());
        }
    } else if ("GT2KML".equals(action)) {

        try {
            GetFileResult result = WebUtils.getSourceFile(request, tempFolderLoc);
            File geoTiffFile = result.getInputFile();
            String fileURL = result.getUrl();

            System.out.println("input GeoTIFF File = " + geoTiffFile.getAbsolutePath());

            String geoTIFFScript = (String) env.lookup("GeoTIFFScript");
            String fwToolsHome = (String) env.lookup("FWToolsHome");

            // send the rdf file
            File kmlFile = WebUtils.convertGTToKML(geoTiffFile, tempFolderLoc, geoTIFFScript, fwToolsHome);
            ResponseHandler.sendTextFileToBrowser(kmlFile, response);
            if (kmlFile.isFile()) {
                //kmlFile.delete();
            }
            //geoTiffFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseHandler.sendError(response, "Error in processing the request. " + e.getMessage());
        }
    }

    // process the shape file to convert to KML
%>
</body>
</html>